# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           muniversal 1.0

name                ghostscript
version             9.50
revision            0
categories          print
license             AGPL-3 BSD
maintainers         nomaintainer
description         GPL Ghostscript, An interpreter for PostScript and PDF
long_description    Ghostscript is the well-known PostScript interpreter which \
                    is available for all common and most esoteric platforms and \
                    supports many different printers and some displays.
platforms           darwin
homepage            https://www.ghostscript.com/
master_sites        https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs[strsed ${version} {g/\.//}]:source \
                    sourceforge:gs-fonts:fonts \
                    https://github.com/adobe-type-tools/mapping-resources-pdf/archive/:misc

# Note: please update ghostscript-fonts-hiragino Portfile to keep version in sync

# Note: this needs to be manually updated for new upstream commits
set mappingresources_commit \
                    a709991e67196a87af053e2c16d7a65108613a41

distname            ghostpdl-${version}
distfiles           ${distname}.tar.gz:source \
                    ghostscript-fonts-other-6.0.tar.gz:fonts \
                    ghostscript-fonts-std-8.11.tar.gz:fonts \
                    ${mappingresources_commit}.zip:misc

patchfiles          patch-base_unix-dll.mak.diff \
                    patch-base_unixinst.mak.diff

checksums           ${distname}.tar.gz \
                    rmd160  9498a55e842736fac6a4f3477b37e5a20da44271 \
                    sha256  dd94c5a06c03c58b47b929d03260f491d4807eaf5be83abd283278927b11c9ee \
                    size    53564956 \
                    ghostscript-fonts-other-6.0.tar.gz \
                    rmd160  ab60dbf71e7d91283a106c3df381cadfe173082f \
                    sha256  4fa051e341167008d37fe34c19d241060cd17b13909932cd7ca7fe759243c2de \
                    size    796086 \
                    ghostscript-fonts-std-8.11.tar.gz \
                    rmd160  10a19a10d0388bc084a7c1d3da845068d7169054 \
                    sha256  0eb6f356119f2e49b2563210852e17f57f9dcc5755f350a69a46a0d641a0c401 \
                    size    3752871 \
                    ${mappingresources_commit}.zip \
                    rmd160  6f327e09590d517760dafd99cc7a197e68d85c6e \
                    sha256  03bb11a4db4b01f8509e93db469a24f904934fece3b5b9b8be932267ed7173f4 \
                    size    1600471

depends_lib         port:expat \
                    port:fontconfig \
                    port:freetype \
                    port:jbig2dec \
                    port:jpeg \
                    port:libiconv \
                    port:libidn \
                    port:libpaper \
                    port:libpng \
                    port:tiff \
                    port:zlib \
                    port:lcms2

depends_build       port:pkgconfig

depends_run         port:perl5

extract.only        ${distname}.tar.gz \
                    ghostscript-fonts-other-6.0.tar.gz \
                    ghostscript-fonts-std-8.11.tar.gz

post-extract {
    system -W ${workpath} "unzip '${distpath}/${mappingresources_commit}.zip'"

    foreach d {freetype jbig2dec jpeg lcms2mt libpng tiff zlib} {
        move ${worksrcpath}/${d} ${worksrcpath}/${d}_local
    }

    #move ${workpath}/MappingOther/Adobe-CNS1-ETen-B5 ${workpath}/MappingOther/Adobe-CNS1-ETenms-B5
    copy -force {*}[glob ${workpath}/mapping-resources-pdf-${mappingresources_commit}/pdf2unicode/*] ${worksrcpath}/Resource/CMap
    copy -force {*}[glob ${workpath}/mapping-resources-pdf-${mappingresources_commit}/pdf2other/*]    ${worksrcpath}/Resource/CMap
}

post-patch {
    reinplace s|__PREFIX__|${prefix}| ${worksrcpath}/base/unix-dll.mak
    reinplace s|ZLIBDIR=src|ZLIBDIR=${prefix}/include| configure.ac

    # Ensure that MacPorts perl is used everywhere
    fs-traverse f ${worksrcpath} {
        if [string match *.pl ${f}] {
            reinplace s|/usr/bin/perl|${prefix}/bin/perl| ${f}
        }
    }
}

# https://trac.macports.org/ticket/56137
configure.ldflags-prepend \
                    -Lsobin

use_autoreconf      yes
autoreconf.args     -fv

# Make included OpenJPEG uses its own headers rather than the system ones
configure.cppflags-replace \
                    -I${prefix}/include \
                    -isystem${prefix}/include

configure.args      --enable-gpdl \
                    --disable-compile-inits \
                    --disable-cups \
                    --disable-dbus \
                    --disable-gtk \
                    --without-pcl \
                    --without-xps \
                    --without-gpdl \
                    --without-x \
                    --without-luratech \
                    --with-system-libtiff

build.target        so

destroot.target     soinstall
post-destroot {
    ln -s gsc ${destroot}${prefix}/bin/gs

    if {[variant_isset ghostpdl]} {
        ln -s gpcl6c ${destroot}/${prefix}/bin/gpcl6
        ln -s gxpsc ${destroot}/${prefix}/bin/gxps
        ln -s gpdlc ${destroot}/${prefix}/bin/gpdl
    }

    xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}
    xinstall -m 0644 -W ${worksrcpath} \
        LICENSE DroidSansFallback.NOTICE \
        ${destroot}/${prefix}/share/doc/${name}

    if {[variant_isset ghostpdl]} {
        xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}/pcl
        xinstall -m 0644 -W ${worksrcpath}/pcl \
            COPYING.AFPL LICENSE NEWS README.txt \
            ${destroot}${prefix}/share/doc/${name}/pcl

        xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}/pcl/pcl
        xinstall -m 0644 -W ${worksrcpath}/pcl/pcl \
            Anomalies.txt \
            ${destroot}${prefix}/share/doc/${name}/pcl/pcl

        xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}/pcl/pxl
        xinstall -m 0644 -W ${worksrcpath}/pcl/pxl \
            pxcet.txt pxdiff.txt pxfts.txt pxlib.txt pxspec.txt \
            ${destroot}${prefix}/share/doc/${name}/pcl/pxl
    }

    # std fonts - install into FontCache-compatible directory.
    # Check: could break on case-sensitive file systems...
    xinstall -m 0755 -d ${destroot}${prefix}/share/fonts/Type1/gsfonts
    xinstall -m 0644 \
            {*}[glob -directory ${workpath}/fonts {[a-z][0-9][0-9][0-9][0-9][0-9][0-9][a-z].*} fonts.scale fonts.dir] \
            ${destroot}${prefix}/share/fonts/Type1/gsfonts

    # Delete the already copied fonts to not copy them again when installing the "other" fonts.
    delete {*}[glob -directory ${workpath}/fonts {[a-z][0-9][0-9][0-9][0-9][0-9][0-9][a-z].*}]

    # other fonts - install into private ghostscript directory.
    xinstall -m 755 -d ${destroot}${prefix}/share/${name}/fonts
    xinstall -m 644 {*}[glob -directory ${workpath}/fonts *.afm *.gsf *.pfa *.pfm] \
            ${destroot}${prefix}/share/${name}/fonts

    # std fonts - "documentation"
    xinstall -m 0755 -d ${destroot}${prefix}/share/doc/${name}/fonts
    xinstall -m 0644 -W ${workpath}/fonts \
            COPYING ChangeLog README README.tweaks TODO \
            ${destroot}${prefix}/share/doc/${name}/fonts

    if {[variant_isset ghostpdl]} {
        # Install PCL base fonts into urwfonts directory
        xinstall -m 755 -d ${destroot}${prefix}/share/${name}/urwfonts
        xinstall -m 644 {*}[glob -directory ${worksrcpath}/pcl/urwfonts *.ttf] \
            ${destroot}${prefix}/share/${name}/urwfonts
    }

    # install missing header
    xinstall -m 0644 ${worksrcpath}/base/gserrors.h \
            ${destroot}${prefix}/include/ghostscript/
}

post-activate {
    system "${prefix}/bin/fc-cache -v ${prefix}/share/fonts/Type1/gsfonts"
}

configure.universal_args-delete --disable-dependency-tracking

variant x11 {
    depends_lib-append      port:xorg-libXext
    depends_lib-append      port:xorg-libXt
    configure.args-replace  --without-x --with-x
}

variant cups description {Enable CUPS driver} {
    configure.args-replace  --disable-cups --enable-cups
}

variant ghostpdl description {Install GhostPDL} {
    license-append          noncommercial
    configure.args-replace  --without-pcl --with-pcl=gpcl6
    configure.args-replace  --without-xps --with-xps=gxps
    configure.args-replace  --without-gpdl --with-gpdl=gpdl
    notes "
    GhostPCL requires a set of truetype fonts to function properly. To use
    the default URW font set, add the following to your shell profile:

        export PCLFONTSOURCE='${prefix}/share/${name}/urwfonts/'

    These fonts are distributed under the Aladdin Free Public License which
    bars commercial use. For more information see
    ${prefix}/share/doc/${name}/pcl/LICENSE
    "
}

default_variants    +x11

livecheck.type      regex
livecheck.url       ${homepage}releases.html
livecheck.regex     ">Ghostscript (\\d+(?:\\.\\d+)*)<"
